package com.uchiha.nova.mainService.controller;

import cn.hutool.core.util.ObjectUtil;
import com.google.common.collect.Lists;

import com.uchiha.nova.core.feign.NavMenuFeignService;
import com.uchiha.nova.core.model.dto.NavMenuDTO;
import com.uchiha.nova.core.model.query.NavMenuQuery;
import com.uchiha.nova.core.model.vo.RoleSelectVO;
import com.uchiha.nova.core.model.vo.TenantNavMenuSelectVO;
import com.uchiha.nova.core.tool.api.R;
import com.uchiha.nova.core.tool.constant.NovaCRUDMethodConstant;
import com.uchiha.nova.core.tool.support.BaseController;
import com.uchiha.nova.core.tool.support.NovaUser;
import com.uchiha.nova.core.tool.support.QueryContext;

import com.uchiha.nova.core.tool.util.BeanToMapUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/navMenu")
@Slf4j
@Tag(name = "侧边栏的前端控制器")
public class NavMenuController extends BaseController {

    @Resource
    NavMenuFeignService navMenuFeignService;

    @Operation(summary = "分页查询")
    @GetMapping(NovaCRUDMethodConstant.NOVA_WEB_PAGE)
    public R getPage(NavMenuQuery query, QueryContext queryContext){
        NovaUser novaUser = getNovaUser();
        query.setTenantId(novaUser.getTenantId());
        List<Object> list = Lists.newArrayList(query,queryContext);
        Map<String,Object> map = BeanToMapUtil.beanToMap(list);
        return navMenuFeignService.getPage(map);
    }

    @Operation(summary = "详情")
    @GetMapping(NovaCRUDMethodConstant.NOVA_WEB_GETONE)
    public R getOne(NavMenuDTO navMenuDTO){
        NovaUser novaUser = getNovaUser();
        navMenuDTO.setTenantId(novaUser.getTenantId());
        return navMenuFeignService.detail(navMenuDTO);
    }

    @Operation(summary = "新增")
    @PostMapping(NovaCRUDMethodConstant.NOVA_WEB_SAVE)
    public R save(@RequestBody NavMenuDTO navMenuDTO){
        if(!navMenuDTO.getPath().startsWith("/")){
            navMenuDTO.setPath("/"+navMenuDTO.getPath());
        }
        return navMenuFeignService.save(navMenuDTO);
    }

    @Operation(summary = "修改")
    @PostMapping(NovaCRUDMethodConstant.NOVA_WEB_UPDATE)

    public R update(@RequestBody NavMenuDTO navMenuDTO){
        if(!navMenuDTO.getPath().startsWith("/")){
            navMenuDTO.setPath("/"+navMenuDTO.getPath());
        }
        return navMenuFeignService.update(navMenuDTO);
    }

    @Operation(summary = "新增或者修改")
    @PostMapping(NovaCRUDMethodConstant.NOVA_WEB_SAVEORUPDATE)
    public R saveOrUpdate(@RequestBody NavMenuDTO navMenuDTO){
        if(!navMenuDTO.getPath().startsWith("/")){
            navMenuDTO.setPath("/"+navMenuDTO.getPath());
        }
        if(ObjectUtil.isNotEmpty(navMenuDTO.getId())){
            if(navMenuDTO.getId().compareTo(navMenuDTO.getParentId())==0){
                return R.fail("不能将菜单设置成自己的子类");
            }
        }
        return navMenuFeignService.saveOrUpdate(navMenuDTO);
    }

    @PostMapping(NovaCRUDMethodConstant.NOVA_WEB_REMOVE)
    @Operation(summary = "逻辑删除")
    public R remove(@RequestParam String ids){
        return navMenuFeignService.remove(ids);
    }
    
    @Operation(summary = "物理删除")
    @DeleteMapping(NovaCRUDMethodConstant.NOVA_WEB_DELETE)
    public R delete(@RequestParam String ids){
        return navMenuFeignService.delete(ids);
    }

    @Operation(summary = "构建树状")
    @GetMapping("/tree")
    public R getTree(@RequestParam String parentId){
        return navMenuFeignService.getTree(Long.valueOf(parentId));
    }

    @Operation(summary = "获取全部树状")
    @GetMapping("/treeAll")
    public R treeAll(){
        return navMenuFeignService.treeAll();
    }

    @Operation(summary = "获取树状选择")
    @GetMapping("/treeDict")
    public R treeDict(){
        return navMenuFeignService.treeDict();
    }

    @Operation(summary = "角色已经选择")
    @GetMapping("/roleHasSelect")
    public R roleHasSelect(@RequestParam Long roleId){
        return navMenuFeignService.roleHasSelect(roleId);
    }

    @Operation(summary = "保存角色选择")
    @PostMapping("/roleSelect")
    public R roleHasSelect(@RequestBody RoleSelectVO roleSelectVO){
        return navMenuFeignService.roleSelect(roleSelectVO);
    }

    @Operation(summary = "租户可以选择")
    @GetMapping("/tenantCanSelect")
    public R tenantCanSelect(){
        NovaUser novaUser = getNovaUser();

        return navMenuFeignService.tenantCanSelect(novaUser.getTenantId());
    }

    @Operation(summary = "租户已经选择")
    @GetMapping("/tenantHasSelect")
    public R tenantHasSelect(@RequestParam String tenantId){
        return navMenuFeignService.tenantHasSelect(tenantId);
    }

    @Operation(summary = "保存租户选择")
    @PostMapping("/tenantNavMenuSelect")
    public R tenantNavMenuSelect(@RequestBody TenantNavMenuSelectVO tenantNavMenuSelectVO){
        return navMenuFeignService.saveTenantSelectVO(tenantNavMenuSelectVO);
    }
}